// pages/mine/order/box_order.js
var App = getApp();
var curentPage = 0;
var hasData = true;
var supplyStatus = 0;//补货订单状态 0未支付，1待收货，2已收货，3已取消
var supplyPage = 0;//补货商城开始页数
var sendOrderNo = '';//确认送达订单号
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0 ,
    orderList: [],
    contentDis: false,
    supplyList: [], //补货订单列表
    noData: true,
    supplyColumn: ['全部','未确认', '待发货', '待收货', '已完成'],
    currentTabSub: 0,
    isManager: false, //是不是管理员
    showTuiHuan:false,
    showConfirm: false //显示确认收货弹窗
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      isManager: wx.getStorageSync('userInfo').havebox==1?true:false,
      supplyList: []
    })

    if (this.data.currentTab == 0) {
      curentPage = 0;
      this.getPurchaseList();
    }else{
      supplyPage = 0;
      this.getSupplyList();
    }
  },

  // 导航切换
  swichNavigator: function (e) {
      var index = e.currentTarget.dataset.current;
      if(index == this.data.currentTab){
        return false;
      }
      this.setData({
        currentTab: index,
      })
      if (this.data.currentTab == 0) {
        curentPage = 0;
        this.setData({
          orderList: []
        })
        this.getPurchaseList();
      } else if (this.data.currentTab == 1) {
        supplyStatus = 0;
        supplyPage = 0;
        this.setData({
          supplyList: []
        })
        this.getSupplyList();
      }

  },

  //云库状态导航切换
  swichSupplyStatus: function (e) {
    var index = e.currentTarget.dataset.current;
    supplyStatus = index;
    supplyPage = 0;
    this.setData({
      currentTabSub: index,
      supplyList: []
    })
    this.getSupplyList();
  },

  onShow: function () {

  },
  onHide: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (hasData) {
      switch (this.data.currentTab) {
        case 0: //盒子订单
          curentPage++;
          this.getPurchaseList();
          break;
        case '1':
        case 1:
          supplyPage++;
          this.getSupplyList();
          break;
        default:
      }
    }

  },
 

  // 订单详情
  goOrderDetail: function (e) {
    let no = e.currentTarget.dataset.no;
    if (this.data.currentTab == 0) { //消费订单
      wx.navigateTo({
        url: 'purchase_detail?order_no=' + no,
      })
    } else if (this.data.currentTab == 1) {
      wx.navigateTo({
        url: 'order_detail?source=list&type=' + this.data.currentTabSub + '&order_no=' + no,
      })
    }

  },
  //获取订单列表
  getPurchaseList: function () {
    let that = this;
    let maxLength = 10;
    wx.request({
      url: App.data.globalAPI + '/order/usersorder/list',
      data: {
        token: wx.getStorageSync('token'),
        page: curentPage,
        length: maxLength
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success(res) {
        if (res.data.error == 0) {
          let originList = that.data.orderList;
          let data = res.data.data;
          if (data.length > 0 || originList.length > 0) {
            originList.push(...data);
            that.setData({
              orderList: originList,
              contentDis: false
            })
          } else {
            that.setData({
              contentDis: true
            })
          }
          if (that.data.orderList.length == 0) {
            that.setData({
              noData: true
            })
            return false;
          } else {
            that.setData({
              noData: false
            })
          }
          if (data.length < maxLength && !that.data.contentDis) {
            hasData = false;
          }

        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 800
          })
        }
      }, fail() {
        wx.showModal({
          title: '提示',
          content: '网络异常，请稍后重试！',
          showCancel: false,
          success(res) {
          }
        })
      }
    })
  },

  //获取补货商城列表
  getSupplyList: function () {
    let that = this;
    let maxLength = 10;
    hasData = true;
    wx.request({
      url: App.data.globalAPI + '/order/supplyorder/list',
      data: {
        token: wx.getStorageSync('token'),
        page: supplyPage,
        length: maxLength,
        order_status: supplyStatus == 0 ? 'all' : (supplyStatus-1)
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success(res) {
        if (res.data.error == 0) {
          let originList = that.data.supplyList;
          let data = res.data.data;
          if (data.length > 0 || originList.length > 0) {
            originList.push(...data);
            that.setData({
              supplyList: originList,
              contentDis: false
            })
          } else {
            that.setData({
              contentDis: true
            })
          }
          if (that.data.supplyList.length == 0) {
            that.setData({
              noData: true
            })
          } else {
            that.setData({
              noData: false
            })
          }
          if (data.length < maxLength && !that.data.contentDis) {
            hasData = false;
            wx.showToast({
              title: '已加载全部数据...',
              icon: 'none',
              duration: 600
            })
          }

        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 800
          })
        }
      }, fail() {
        wx.showModal({
          title: '提示',
          content: '网络异常，请稍后重试！',
          showCancel: false,
          success(res) {
          }
        })
      }
    })
  },

  // 退换货的显示关闭
  tuihuanHandle:function(){
    this.setData({
      showTuiHuan: !this.data.showTuiHuan
    })
  },
  //退换盒联系客服
  lianxi: function () {
    this.setData({
      showTuiHuan: false
    });
    wx.makePhoneCall({
      phoneNumber: '18131109842' // 仅为示例，并非真实的电话号码
    })
  },

  // 取消订单
  onCancelOrder: function (e) {
    let order_no = e.currentTarget.dataset.no;
    let that = this;
    wx.request({
      url: App.data.globalAPI + '/order/supplyorder/cancel',
      data: {
        token: wx.getStorageSync('token'),
        order_no: order_no,
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success(res) {
        if (res.data.error == 0) {
          wx.showToast({
            title: '订单取消成功!',
            duration: 800
          })
          setTimeout(function () {
            that.setData({
              supplyList:[],
            })
            that.getSupplyList();
          }, 1000)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 800
          })
        }
      }
    })
  },

  // 确认送达弹窗
  onShowSend:function(e){
    sendOrderNo = e.currentTarget.dataset.no;
    this.setData({
      showConfirm:true
    })
  },

  // 关闭确认送达
  closeSendWin:function(){
    this.setData({
      showConfirm: false
    })
  },

  // 确认送达
  onSendHandle:function(){
    let that = this;
    wx.request({
      url: App.data.globalAPI + '/order/users/confirm',
      data: {
        token: wx.getStorageSync('token'),
        order_no: sendOrderNo,
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success(res) {
        if (res.data.error == 0) {
          that.setData({
            supplyList: [],
            showConfirm: false 
          })
          that.getSupplyList();
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 800
          })
        }
      }
    })
  }

})